package org.zju.demo.shoppingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.model.Address;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;

/**
 * Created by ZJDX on 2015/9/11.
 */
public class AddAddressActivity extends CommonActivity {

    private EditText nameEditText;
    private EditText telEditText;
    private EditText zipEditText;
    private EditText detailEditText;
    private FrameLayout addFrame;

    private AddAddressListener listener = new AddAddressListener();

    Intent startIntent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不显示标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.usercenter_address_new);
        //First read data from intent
        startIntent = this.getIntent();

        //Second bind component
        nameEditText = (EditText) findViewById(R.id.add_address_name);
        telEditText = (EditText) findViewById(R.id.add_address_telNum);
        zipEditText = (EditText) findViewById(R.id.add_address_zipCode);
        detailEditText = (EditText) findViewById(R.id.add_address_detail);
        addFrame = (FrameLayout) findViewById(R.id.add_address_add);
        addFrame.setOnClickListener(listener);
    }

    private Address readData() {
        Address address = new Address();
        String tmp = nameEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            Toast.makeText(this, "收货人姓名不能为空", Toast.LENGTH_SHORT).show();
            return null;
        }
        address.setCustomName(tmp);
        tmp = telEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            Toast.makeText(this, "联系方式不能为空", Toast.LENGTH_SHORT).show();
            return null;
        }
        address.setTelNumber(tmp);
        tmp = zipEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            Toast.makeText(this, "邮编不能为空", Toast.LENGTH_SHORT).show();
            return null;
        }
        address.setZipCode(tmp);
        tmp = detailEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            Toast.makeText(this, "收货地址不能为空", Toast.LENGTH_SHORT).show();
            return null;
        }
        address.setAddress(tmp);
        address.setUsername(startIntent.getStringExtra("username"));
        return address;
    }

    class AddAddressListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.add_address_add:
                    Address address = readData();
                    if (address != null) {
                        address.save();
                        long id = address.getId();
                        Intent result = new Intent();
                        result.putExtra("id", Long.toString(id));
                        setResult(RESULT_OK, result);
                        finish();
                    }
                    break;
                default:
            }
        }
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        super.onResponseMessage(response);
    }

    @Override
    public void initBottomMenu() {
    }
}
